package org.gov.vis.master.controller;

import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.master.dao.OtherDao;
import org.gov.vis.master.dao.OtherDaoImpl;
import org.gov.vis.master.dto.OtherDto;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

public class OtherCtlrImpl  implements OtherCtlr{

	final private ResourceBundle rb = Util.getResources();

	/**
	 * Method called to Add or Edit the Other
	 * @param otherDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditOther(final OtherDto _otherDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _otherCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_othr_code"), _otherDto.getCode(), 1, 20, true);
			if(_otherCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_othr_code").toUpperCase(), _otherCodeErr);

			String _otherNameOrgErr = ValidatorUtils.validateString(rb.getString("dialog_label_othr_nmeoforg"), _otherDto.getNameOfOrg(), 5, 50, true);
			if(_otherNameOrgErr != null)
				errorMsgs.put(rb.getString("dialog_label_othr_nmeoforg").toUpperCase(), _otherNameOrgErr);
			
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			
			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists another Other Object with the same Code
			//If Code has been duplicated, then throw the alert
			OtherDto otherDto = null;
			if(_otherDto.getOtherId() > 0){
				//get the OtherDto on the passed Code name
				otherDto = this.getOtherOnCode(_otherDto.getCode());
				if(otherDto != null && otherDto.getOtherId() != _otherDto.getOtherId()){
					errorMsgs.put(rb.getString("dialog_label_othr_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_OTHER_MSG));
				}
			}//end if
			else{
				//get the OtherDto on the passed Code name
				otherDto = this.getOtherOnCode(_otherDto.getCode());
				if(otherDto != null){
					errorMsgs.put(rb.getString("dialog_label_othr_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_OTHER_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final OtherDao _otherDao = new OtherDaoImpl();
			//Method called to either Add or Edit a Other
			_otherDao.addOrEditOther(_otherDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			if(_otherDto.getOtherId() > 0){
				throw new SystemException(rb.getString(GuiConstants.MASTER_EDIT_OTHER_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.MASTER_ADD_OTHER_EXCEPTION));
			}
		}
	}//end addOrEditOther

	/**
	 * Method called to delete the Other Object based on the passed other Id
	 * @param _otherId
	 * @throws Exception
	 */
	@Override
	public void deleteOther(final long _otherId) throws Exception {
		try{
			final OtherDao _otherDao = new OtherDaoImpl();
			_otherDao.deleteOther(_otherId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_DELETE_OTHER_EXCEPTION));
		}
	}//end deleteOther

	/**
	 * Method called to get the Other based on the passed primary key Id
	 * @param _otherId
	 * @return
	 * @throws Exception
	 */
	@Override
	public OtherDto getOther(final long _otherId) throws Exception {
		try{
			final OtherDao _otherDao = new OtherDaoImpl();
			OtherDto _otherDto = new OtherDto();
			_otherDto.setOtherId(_otherId);

			OtherDto otherDto = null;
			List<OtherDto> _otherList = _otherDao.getOthers(_otherDto);
			if(_otherList != null && !_otherList.isEmpty()){
				otherDto = _otherList.get(0);
			}

			return otherDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_EDIT_OTHER_EXCEPTION));
		}
	}//end getOther

	/**
	 * Method called to get the Other based on the passed Code
	 * @param _otherCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public OtherDto getOtherOnCode(final String _code) throws Exception {
		final OtherDao _otherDao = new OtherDaoImpl();
		OtherDto _otherDto = new OtherDto();
		_otherDto.setCode(_code);

		OtherDto otherDto = null;
		List<OtherDto> _otherList = _otherDao.getOthers(_otherDto);
		if(_otherList != null && !_otherList.isEmpty()){
			otherDto = _otherList.get(0);
		}

		return otherDto;
	}//end getOtherOnCode

	/**
	 * Method called to get the Other based on the passed Other Object
	 * @param otherDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public OtherDto getOther(final OtherDto _otherDto) throws Exception {
		final OtherDao _otherDao = new OtherDaoImpl();

		OtherDto otherDto = null;
		List<OtherDto> _otherList = _otherDao.getOthers(_otherDto);
		if(_otherList != null && !_otherList.isEmpty()){
			otherDto = _otherList.get(0);
		}

		return otherDto;
	}//end getOthers

	/**
	 * Method called to get the list of Other based on the passed Other Object
	 * @param otherDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OtherDto> getOthers(final OtherDto _otherDto) throws Exception {
		final OtherDao _otherDao = new OtherDaoImpl();
		return _otherDao.getOthers(_otherDto);
	}//end getOthers

	/**
	 * Method called to search for the Others based on the passed Other Object
	 * @param otherDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OtherDto> searchOthers(OtherDto _otherDto) throws Exception {
		try{
			final OtherDao _otherDao = new OtherDaoImpl();
			return _otherDao.searchOthers(_otherDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_MAINTAIN_OTHER_EXCEPTION));
		}
	}//end searchOthers

}//end class
